package com.wnxy.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnxy.admin.common.handler.Asserts;
import com.wnxy.admin.common.handler.BusinessEnum;
import com.wnxy.admin.dto.CustomerDto;
import com.wnxy.admin.dto.RegisterDto;
import com.wnxy.admin.entity.Customer;
import com.wnxy.admin.entity.Reservation;
import com.wnxy.admin.mapper.CustomerMapper;
import com.wnxy.admin.service.ICustomerService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    private final CustomerMapper customerMapper;
    private final RedisTemplate redisTemplate;

    public CustomerServiceImpl(CustomerMapper customerMapper, RedisTemplate redisTemplate) {
        this.customerMapper = customerMapper;
        this.redisTemplate = redisTemplate;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterDto registerDto) {
        //1判断验证码是否一致
        String code = (String) redisTemplate.opsForValue().get(registerDto.getEmail());
        Asserts.fail(!StringUtils.hasLength(code),BusinessEnum.VALICODE_EXPIRED);
        Asserts.fail(!code.equals(registerDto.getValicode()),BusinessEnum.VALICODE_IS_ERROR);
        //判断账号是否已存在
        boolean flag = customerMapper.exists(Wrappers.lambdaQuery(Customer.class).eq(Customer::getAccount, registerDto.getAccount()));
        Asserts.fail(flag == true, BusinessEnum.ACCOUNT_EXISTS);
        //判断手机号是否已存在
        flag = customerMapper.exists(Wrappers.lambdaQuery(Customer.class).eq(Customer::getPhone, registerDto.getPhone()));
        Asserts.fail(flag == true, BusinessEnum.PHONE_EXISTS);
        //加入数据库
        Customer customer = BeanUtil.copyProperties(registerDto, Customer.class);
        customerMapper.insert(customer);
        //删除验证码
        redisTemplate.delete(registerDto.getEmail());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editService(Reservation reservation, Integer service) {
        customerMapper.update(null, Wrappers.lambdaUpdate(Customer.class)
                .eq(Customer::getId, reservation.getCustomerId())
                .set(Customer::getService, service));
    }

    @Override
    public void editService(Reservation reservation) {

    }

    @Override
    public PageInfo<CustomerDto> getAll(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<CustomerDto> customerDtos = customerMapper.selectAll(keyword);
        return new PageInfo<>(customerDtos);
    }


}
